﻿using System;

using DotNetty.Transport.Channels;

namespace SimbaRGVMonitor.Common.Net
{
    partial class MonitorTcpClient
    {
        class ChannelHandler : ChannelHandlerAdapter
        {
            public override bool IsSharable
            {
                get { return true; }
            }

            //====================================================================================================
            //以上是类型属性
            //----------------------------------------------------------------------------------------------------
            //以下是类型事件及触发函数
            //====================================================================================================

            public event EventHandler<DotNettyChannelEventArgs> ChannelActived;
            private void OnChannelActived(IChannel channel)
            {
                var e = new DotNettyChannelEventArgs(channel);
                ChannelActived.TriggerEvent(this, e);
            }

            public event EventHandler<DotNettyChannelEventArgs> ChannelInactived;
            private void OnChannelInactived(IChannel channel)
            {
                var e = new DotNettyChannelEventArgs(channel);
                ChannelInactived.TriggerEvent(this, e);
            }

            public event EventHandler<DotNettyChannelDataReceivedEventArgs> ChannelDataReceived;
            private void OnChannelDataReceived(IChannel channel, object data)
            {
                var e = new DotNettyChannelDataReceivedEventArgs(channel, data);
                ChannelDataReceived.TriggerEvent(this, e);
            }

            public event EventHandler<DotNettyChannelExceptionCaughtEventArgs> ChannelExceptionCaught;
            private void OnChannelExceptionCaught(IChannel channel, Exception ex)
            {
                var e = new DotNettyChannelExceptionCaughtEventArgs(channel, ex);
                ChannelExceptionCaught.TriggerEvent(this, e);
            }

            //====================================================================================================
            //以上是类型事件及触发函数
            //----------------------------------------------------------------------------------------------------
            //以下是重写信道事件
            //====================================================================================================

            public override void ChannelActive(IChannelHandlerContext context)
            {
                var channel = context.Channel;
                OnChannelActived(channel);
            }

            public override void ChannelInactive(IChannelHandlerContext context)
            {
                var channel = context.Channel;
                OnChannelInactived(channel);
            }

            public override void ChannelRead(IChannelHandlerContext context, object message)
            {
                var channel = context.Channel;
                OnChannelDataReceived(channel, message);
            }

            public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
            {
                var channel = context.Channel;
                OnChannelExceptionCaught(channel, exception);
            }
        }
    }
}